package app.ep1.bandejoes;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONObject;

public class OpiniaoBandejao {
	private String texto;
	private Date hora;
	private TamanhoFila fila = null;

	public OpiniaoBandejao() {
	}

	public OpiniaoBandejao(JSONObject json) {
		this.texto = json.optString("texto");

		String fila = json.optString("fila");
		if (fila == null || fila.compareTo("null") == 0)
			this.fila = TamanhoFila.SEM_FILA;
		else
			this.fila = TamanhoFila.valueOf(fila);

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"ss-mm-HH-dd-MM-yy");

		try {
			this.hora = simpleDateFormat.parse(json.optString("hora"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public String getTexto() {
		return texto;
	}

	public void setTexto(String texto) {
		this.texto = texto;
	}

	public Date getData() {
		return hora;
	}

	public void setData(Date data) {
		this.hora = data;
	}

	public void setData() {
		this.hora = new Date();
	}

	public TamanhoFila getFila() {
		return fila;
	}

	public void setFila(TamanhoFila fila) {
		this.fila = fila;
	}

	public void setFila(String fila) {
		fila = transformaStringParaTamanhoFila(fila);

		this.fila = TamanhoFila.valueOf(fila);
	}

	private String transformaStringParaTamanhoFila(String string) {
		string = string.toUpperCase();
		string = string.replaceAll(" ", "_");

		string = string.replaceAll("[ÂÀÁÄÃ]", "A");
		string = string.replaceAll("[âãàáä]", "a");
		string = string.replaceAll("[ÊÈÉË]", "E");
		string = string.replaceAll("[êèéë]", "e");
		string = string.replaceAll("ÎÍÌÏ", "I");
		string = string.replaceAll("îíìï", "i");
		string = string.replaceAll("[ÔÕÒÓÖ]", "O");
		string = string.replaceAll("[ôõòóö]", "o");
		string = string.replaceAll("[ÛÙÚÜ]", "U");
		string = string.replaceAll("[ûúùü]", "u");
		string = string.replaceAll("Ç", "C");
		string = string.replaceAll("ç", "c");
		string = string.replaceAll("[ýÿ]", "y");
		string = string.replaceAll("Ý", "Y");
		string = string.replaceAll("ñ", "n");
		string = string.replaceAll("Ñ", "N");
		string = string.replaceAll("['<>\\|/]", "");

		return string;
	}
}
